//
//  XWNetwokTools.swift
//  新浪 weibo
//
//  Created by xuwei on 16/1/21.
//  Copyright © 2016年 xuwei. All rights reserved.
//

import UIKit
import AFNetworking

enum XWRequestMethod: String{
    case GET = "GET"
    case POST = "POST"
    
}

class XWNetworkTools: AFHTTPSessionManager {
   
    static let sharedTools: XWNetworkTools = {
        let tools = XWNetworkTools()
         tools.responseSerializer.acceptableContentTypes?.insert("text/plain")
        return tools
    }()
    
    
    
    
    func request(method: XWRequestMethod = .GET,urlString: String, parameters: AnyObject?, finished:(response: AnyObject?, error: NSError?)->()){
        // 定义请求成功的闭包
        let success = {(dataTask: NSURLSessionDataTask, response: AnyObject?) -> Void in
            finished(response: response, error: nil)
        }
        
        // 定义请求失败的闭包
        let failure = {(dataTask: NSURLSessionDataTask?, error: NSError) -> Void in
            finished(response: nil, error: error)
        }
        
        // 根据请求方式,发送不同的请求
        if method == .GET {
            GET(urlString, parameters: parameters, progress: nil, success: success, failure: failure)
        }else{
            POST(urlString, parameters: parameters, progress: nil, success: success, failure: failure)
        }
    }


}
